//
//  CareAboutOthersController.m
//  healthApp
//
//  Created by william mu on 12-8-29.
//  Copyright (c) 2012年 Tencent. All rights reserved.
//

#import "CareAboutOthersController.h"
#import "FriendUpdateCell.h"
#import "FriendDetailController.h"
#import "ProtocolTranslate.h"

@interface CareAboutOthersController ()

@end

@implementation CareAboutOthersController
@synthesize tableView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"关心朋友";

}

- (void)viewWillAppear:(BOOL) animated
{
    m_sharedSymptomRes = 0;
    [m_friendSymptom release];
    m_friendSymptom = [[NSMutableDictionary dictionary] retain];
    [m_userBaseInfo release];
    m_userBaseInfo = [[NSMutableDictionary dictionary] retain];
    [m_allRequest release];
    m_allRequest = [[NSMutableArray array] retain];
    [self startRequest];
}

-(void) viewWillDisappear:(BOOL)animated
{
    ProtocolService* protocol = [ProtocolService sharedInstance];
    [protocol shutdownAnyResponse:m_allRequest];
}

-(void) startRequest
{
    GlobalState* state = [GlobalState sharedInstance];
    ProtocolService* protocol = [ProtocolService sharedInstance];
    [protocol setTarget:self];
    [protocol setAction:@selector(onGetUserFriends:)];
    NSString* seq = [protocol getUserFriends:state.uid];
    [m_allRequest addObject:seq];
}

-(void) onGetUserFriends:(NSString*) json
{
    [m_userFriends release];
    
    m_userFriends = (NSArray*)[[json JSONValue] retain];
    
    ProtocolService* protocol = [ProtocolService sharedInstance];
    
    for(NSString* friendUid in m_userFriends)
    {
        [protocol setAction:@selector(onGetFriendSharedSymptom:)];
        [protocol setTarget:self];
        NSString* seq = [protocol getSharedSymptom:friendUid];
        [m_allRequest addObject:seq];
    }
}

-(void) onGetFriendSharedSymptom:(NSString*) json
{
    NSArray* symptom = (NSArray*)[json JSONValue];

    if(!symptom)
        return;
    
    m_sharedSymptomRes ++;
    
    NSString* friendUid = [[symptom lastObject]objectForKey:@"uid"];
    
    [m_friendSymptom setObject:symptom forKey:friendUid];
    
    ProtocolService* protocol = [ProtocolService sharedInstance];
    [protocol setTarget:self];
    [protocol setAction:@selector(onGetFriendBaseInfo:)];
    NSString* seq = [protocol requestUserBaseInfo:friendUid];
    [m_allRequest addObject:seq];
    
}

-(void) onGetFriendBaseInfo:(NSString*) json
{
    NSDictionary* baseInfo = (NSDictionary*)[json JSONValue];
    NSString* friendUid = [baseInfo objectForKey:@"uid"];
    
    [m_userBaseInfo setObject:baseInfo forKey:friendUid];
    
    if([m_userBaseInfo count] == m_sharedSymptomRes)
        [self.tableView reloadData];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    self.tableView = nil;
    [m_friendSymptom release], m_friendSymptom = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


-(NSString*) getFriendUidForIndexPath:(NSIndexPath*) indexPath
{
    NSUInteger row = [indexPath row];
    NSArray* allKeys = [m_friendSymptom allKeys];
    NSString* friendUid = [allKeys objectAtIndex:row];
    
    return friendUid;
}

-(NSString*) getSymptomStringForIndexPath:(NSIndexPath*) indexPath font:(UIFont*) font
{
    
    NSString* friendUid = [self getFriendUidForIndexPath:indexPath];
    
    NSArray* symptoms = [m_friendSymptom objectForKey:friendUid];
    
    NSString* final = get_symptoms_desc(symptoms, 10);
    
    return final;
}

-(NSString*) getFriendNameForIndexPath:(NSIndexPath*) indexPath
{
    NSString* friendUid = [self getFriendUidForIndexPath:indexPath];
    GlobalState* state = [GlobalState sharedInstance];
    NSDictionary* baseInfo = [m_userBaseInfo objectForKey:friendUid];
    NSString* lastName = [baseInfo objectForKey:@"lastName"];
    NSString* firstName = [baseInfo objectForKey:@"firstName"];
    NSString* friendName = [NSString stringWithFormat:@"%@%@", lastName, firstName];
    
    return friendName;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [m_friendSymptom count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FriendUpdateCell* cell = [self.tableView dequeueReusableCellWithIdentifier:@"FriendUpdateCell"];
    
    if(!cell)
    {
        NSArray* ary = (NSArray*)[[NSBundle mainBundle] loadNibNamed:@"FriendUpdateCell" owner:self options:nil];
        
        cell = [ary objectAtIndex:0];
    }
    
    cell.friendName.text = [self getFriendNameForIndexPath:indexPath];
    cell.friendSymptom.text = [self getSymptomStringForIndexPath:indexPath font:cell.friendSymptom.font];
    cell.friendUid = [self getFriendUidForIndexPath:indexPath];
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    FriendDetailController* controller = [[FriendDetailController alloc] init];
    
    FriendUpdateCell* cell = [tableView cellForRowAtIndexPath:indexPath];
    
    NSArray* symptoms = [m_friendSymptom objectForKey:cell.friendUid];
    
    controller.Param = [NSDictionary dictionaryWithObject:symptoms forKey:@"symptoms"];
    
    [self.navigationController pushViewController:controller animated:YES];
    
    [controller release];
}



@end
